/*
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 * 
 * You should have received a copy of the GNU Lesser General Public
 * License along with main.c; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor Boston, MA 02110-1301,  USA
 */
 
#include <clutter/clutter.h>
#include <stdlib.h>

ClutterActor *stage = NULL;
GSList *list_items = 0;
const gint IMAGE_HEIGHT = 75;
ClutterTimeline *resize = NULL;

ClutterActor *current_big = NULL;

typedef struct Item
{
		ClutterActor *actor;
		gchar* filepath;
} Item;

void on_foreach_clear_list_items(gpointer data, gpointer user_data)
{
  Item* item = (Item*)data;

  /* We don't need to unref the actor because the floating reference was taken by the stage. */
  g_free (item->filepath);
  g_free (item);
}

static gboolean
on_texture_button_press (ClutterActor *actor, ClutterEvent *event, gpointer user_data)
{
		
		if (!current_big) /* this is the first zoom */
		{
				current_big = actor;
				clutter_actor_set_size (actor, 225, 225);
		}
		
		else if (current_big == actor) /* we clicked the currently zoom'd */ 
		{
				clutter_actor_set_size (actor, 75, 75);
				current_big = NULL;
		}
		else if (current_big && current_big != actor)
		{
				clutter_actor_set_size (actor, 225, 225);
				clutter_actor_set_size (current_big, 75, 75);
				current_big = actor;
		}
		
		return TRUE;
}

void scale_texture_default(ClutterActor *texture)
{
  int pixbuf_height = 0;
  clutter_texture_get_base_size (CLUTTER_TEXTURE (texture), NULL, &pixbuf_height);

  const gdouble scale = pixbuf_height ? IMAGE_HEIGHT /  (gdouble)pixbuf_height : 0;
  clutter_actor_set_scale (texture, scale, scale);
}


void load_images (const gchar* directory_path)
{
		g_return_if_fail (directory_path);
		
		/* Clear existing images */
		g_slist_foreach (list_items, on_foreach_clear_list_items, NULL);
		g_slist_free (list_items);
		
		list_items = NULL;
		
		/* Find the images in path */
		GError *error = NULL;
		GDir* dir = g_dir_open (directory_path, 0, &error);
		if (error)
		{
				g_warning ("g_dir_open() failed %s\n", error->message);
				g_clear_error (&error);
				return;
		}
		
		const gchar* filename = NULL;
		while ( (filename = g_dir_read_name (dir)) )
		{
				gchar* path = g_build_filename (directory_path, filename, NULL);
				
				/* Load the file as an image actor */
				ClutterActor *actor = clutter_texture_new_from_file (path, NULL);
				if (actor)
				{
						Item* item = g_new0 (Item, 1);
						item->actor = actor;
						item->filepath = g_strup (path);

						/* Adjust the height */
						scale_texture_default (item->actor);
						
						list_items = g_slist_append (list_items, item);
				}
				
				g_free (path);
		}
}

void add_image_actors ()
{
		int x = 20;
		int y = 20;
		GSList *list = list_items;
		
		int total_width = 600-x;
		
		while (list)
		{
				Item *item = (Item*)list->data;
				ClutterActor *actor = item->actor;
				clutter_container_add_actor (CLUTTER_CONTAINER (stage), actor);
				
				/* Initial Position */
				if (total_width < 95)
				{ /* We don't have enough room for a new image */
						y += 95; /* 20 border + 75 image */
						x = 20;  /* Back to the left margin */
						total_width = 580; /* 580 pixels left to draw */
						
						clutter_actor_set_position (actor, x, y);
				}
				else
				{
						clutter_actor_set_position (actor, x, y);
						total_width -= 95; /* 20 border + 75 image */
						x += 95;
				}
				
				/* Add the callback */
				clutter_actor_set_reactive (actor, TRUE);
				g_signal_connect (actor, "button-press-event",
													G_CALLBACK (on_texture_button_press), item);
				
				clutter_actor_show (actor);
				
				list = g_slist_next (list);
		}
}

int main (int argc, char *argv[])
{
		ClutterColor stage_color = { 0xB0, 0xB0, 0xB0, 0xff };
		clutter_init (&argc, &argv);
		
		stage = clutter_stage_get_default ();
		clutter_actor_set_size (stage, 600, 450);
		clutter_stage_set_color (CLUTTER_STAGE (stage), &stage_color);
		
		clutter_actor_show (stage);
		
		load_images ("./images/");
		add_image_actors ();
		
		clutter_main ();
		
		/* Free some stuff */
		g_slist_foreach(list_items, on_foreach_clear_list_items, NULL);
		g_slist_free (list_items);
		
		return EXIT_SUCCESS;
}
